<?php
//use Com;
/**
 * Created by PhpStorm.
 * User: poptser
 * Email:poptser@foxmail.com
 * Date: 2016/8/11
 * Time: 13:50
 */

function img_init($img){
    if(empty($img)){
        $img='Public/images1/prd.jpg';
    }
    return $img;
}

/**
 * 获得指定分类下所有底层分类的ID
 * @param type $str
 * @return type       
 */
 
function get_children_cat($str) {
    $act_id = explode(',', $str);
    foreach ($act_id as $val) {
        $cat[] = array_unique(array_merge(array($val), array_keys(cat_list($val, 0, false))));
    }
    foreach ($cat as $key => $val) {
        foreach ($val as $k => $v) {
            $res[] = $v;
        }
    }
    return array_unique($res);
}
/**
 * 创建像这样的查询: "IN('a','b')";
 *
 * @access   public
 * @param    mix      $item_list      列表数组或字符串
 * @param    string   $field_name     字段名称
 *
 * @return   void
 */
function db_create_in($item_list, $field_name = '') {
    if (empty($item_list)) {
        return $field_name . " IN ('') ";
    } else {
        if (!is_array($item_list)) {
            $item_list = explode(',', $item_list);
        }
        $item_list = array_unique($item_list);
        $item_list_tmp = '';
        foreach ($item_list AS $item) {
            if ($item !== '') {
                $item_list_tmp .= $item_list_tmp ? ",'$item'" : "'$item'";
            }
        }
        if (empty($item_list_tmp)) {
            return $field_name . " IN ('') ";
        } else {
            return $field_name . ' IN (' . $item_list_tmp . ') ';
        }
    }
}
/**
 * 截取字符串，字节格式化
 * @param unknown $str
 * @param unknown $length
 * @param number $start
 * @param string $charset
 * @param string $suffix
 * @return string
 */
function msubstr($str, $length, $start = 0, $charset = "utf-8", $suffix = true) {
    if (function_exists("mb_substr"))
        $slice = mb_substr($str, $start, $length, $charset);
    elseif (function_exists('iconv_substr')) {
        $slice = iconv_substr($str, $start, $length, $charset);
    } else {
        $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("", array_slice($match[0], $start, $length));
    }
    return $suffix ? $slice . '...' : $slice;
}

// php获取当前访问的完整url地址
function GetCurUrl() {
    $url = 'http://';
    if (isset ( $_SERVER ['HTTPS'] ) && $_SERVER ['HTTPS'] == 'on') {
        $url = 'https://';
    }
    if ($_SERVER ['SERVER_PORT'] != '80') {
        $url .= $_SERVER ['HTTP_HOST'] . ':' . $_SERVER ['SERVER_PORT'] . $_SERVER ['REQUEST_URI'];
    } else {
        $url .= $_SERVER ['HTTP_HOST'] . $_SERVER ['REQUEST_URI'];
    }
    // 兼容后面的参数组装
    if (stripos ( $url, '?' ) === false) {
        $url .= '?t=' . time ();
    }
    return $url;
}
//// 获取当前用户的OpenId
//function get_openid($options) {
//    $auth = new Com\Wxauth($options);
//    $openid =  $auth->open_id;
//    session ( 'wxuser_'.$openid, $auth->wxuser );     //wxauth获得的微信用户信息存到session中
//    if (empty ( $openid )) {
//        return - 1;
//    }
//    return $openid;
//}
/**
 * 验证输入的邮件地址是否合法
 */
function is_email($user_email){
    $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
    if (strpos($user_email, '@') !== false && strpos($user_email, '.') !== false) {
        if (preg_match($chars, $user_email)) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

/**
 * 验证输入的手机号是否合法
 */
function is_mobile($user_mobile) {
    $chars = '/^13[0-9]{9}|15[012356789][0-9]{8}|18[0-9]{9}|14[579][0-9]{8}|17[0-9]{9}$/';
    if (preg_match($chars, $user_mobile)) {
        return true;
    } else {
        return false;
    }
}

/**
 * 检查是否为一个合法的时间格式
 *
 * @access  public
 * @param   string  $time
 * @return  void
 */
function is_time($time){
    $pattern = '/[\d]{4}-[\d]{1,2}-[\d]{1,2}\s[\d]{1,2}:[\d]{1,2}:[\d]{1,2}/';
    return preg_match($pattern, $time);
}

/**
 * 编译密码函数
 *
 * @access public
 * @param array $cfg
 * 包含参数为 $password, $md5password, $salt, $type
 *
 * @return void
 */
function compile_password($cfg){
    if (isset($cfg['password'])) {
        $cfg['md5password'] = md5($cfg['password']);
    }
    if (empty($cfg['type'])) {
        $cfg['type'] = 1;
    }
    switch ($cfg['type']) {
        case 1:
            if (! empty($cfg['ec_salt'])) {
                return md5($cfg['md5password'] . $cfg['ec_salt']);
            } else {
                return $cfg['md5password'];
            }

        case 2:
            if (empty($cfg['salt'])) {
                $cfg['salt'] = '';
            }
            return md5($cfg['salt'] . $cfg['md5password']);
        case 3:
            if (empty($cfg['salt'])) {
                $cfg['salt'] = '';
            }
            return md5($cfg['md5password'] . $cfg['salt']);
        default:
            return '';
    }
}

/**
 * 获得用户的真实IP地址
 *
 * @access  public
 * @return  string
 */
function get_ip()
{
    static $realip = NULL;
    if ($realip !== NULL) {
        return $realip;
    }
    if (isset($_SERVER)) {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
            foreach ($arr AS $ip) {
                $ip = trim($ip);
                if ($ip != 'unknown') {
                    $realip = $ip;
                    break;
                }
            }
        } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $realip = $_SERVER['HTTP_CLIENT_IP'];
        } else {
            if (isset($_SERVER['REMOTE_ADDR'])) {
                $realip = $_SERVER['REMOTE_ADDR'];
            } else {
                $realip = '0.0.0.0';
            }
        }
    } else {
        if (getenv('HTTP_X_FORWARDED_FOR')) {
            $realip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif (getenv('HTTP_CLIENT_IP')) {
            $realip = getenv('HTTP_CLIENT_IP');
        } else {
            $realip = getenv('REMOTE_ADDR');
        }
    }
    preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
    $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
    return $realip;
}

// 判断是否是在微信浏览器里
function isWeixinBrowser() {
    $agent = $_SERVER ['HTTP_USER_AGENT'];
    if (! strpos ( $agent, "icroMessenger" )) {
        return false;
    }
    return true;
}
/**
 * 数组转换为字符串，主要用于把分隔符调整到第二个参数
 * @param  array  $arr 要连接的数组
 * @param  string $glue 分割符
 * @return string
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function arr2str($arr, $glue = ',')
{
    return implode($glue, $arr);
}

	
/**
 * 判断某个商品是否正在特价促销期
 *
 * @access  public
 * @param   float   $price      促销价格
 * @param   string  $start      促销开始日期
 * @param   string  $end        促销结束日期
 * @return  float   如果还在促销期则返回促销价，否则返回0
 */
function bargain_price($price, $start, $end) {
    if ($price == 0) {
        return 0;
    } else {
        $time = gmtime();

        if($start < $time && $end > $time){
            return $price;
        }else{
            return 0;
        }
    }
}

/**
 * 格式化商品价格
 *
 * @access  public
 * @param   float   $price  商品价格
 * @return  string
 */
function price_format($price, $change_price = true) {
    if ($price === '') {
        $price = 0;
    }
    $price = 0 + $price;//添加这一行，转换成数值
    if ($change_price) {
        switch (4) {
            case 0:
                $price = number_format($price, 2, '.', '');
                break;
            case 1: // 保留不为 0 的尾数
                $price = preg_replace('/(.*)(\\.)([0-9]*?)0+$/', '\1\2\3', number_format($price, 2, '.', ''));

                if (substr($price, -1) == '.') {
                    $price = substr($price, 0, -1);
                }
                break;
            case 2: // 不四舍五入，保留1位
                $price = substr(number_format($price, 2, '.', ''), 0, -1);
                break;
            case 3: // 直接取整
                $price = intval($price);
                break;
            case 4: // 四舍五入，保留 1 位
                $price = number_format($price, 1, '.', '');
                break;
            case 5: // 先四舍五入，不保留小数
                $price = round($price);
                break;
        }
    } else {
        $price = number_format($price, 2, '.', '');
    }

    $price = sprintf('￥%s', $price);

    return $price = strip_tags($price);
}


function get_price($price){
    if($price>0){
        $res = round($price,2);
    }else{
        $res = 0;
    }
    return number_format($res, 2, '.', '');
}
/**
 * 重新获得商品图片与商品相册的地址
 *
 * @param int $goods_id 商品ID
 * @param string $image 原商品相册图片地址
 * @param boolean $thumb 是否为缩略图
 * @param string $call 调用方法(商品图片还是商品相册)
 * @param boolean $del 是否删除图片
 *
 * @return string   $url
 */
function get_image_path($goods_id, $image = '', $thumb = false, $call = 'goods', $del = false) {
    $url = C('no_picture');
    if (!empty($image)) {
        if (strtolower(substr($image, 0, 4)) == 'http') {
            return $image;
        }
        $shop_url = rtrim(C('shop_url'));
        $do_main=get_domain();
        $root=rtrim(dirname($_SERVER["SCRIPT_NAME"]), '\\/');
        $base_url = empty($shop_url) ? dirname($do_main.$root):$shop_url;
        $url = $base_url . '/' . $image;
    }
    return $url;
}

/**
 * 取得当前的域名
 * @return string
 */
function get_domain() {
    /* 协议 */
    $protocol = (isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off') && !empty($_SERVER['HTTPS'])) ? 'https://' : 'http://';
    /* 域名或IP地址 */
    if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
        $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
    } elseif (isset($_SERVER['HTTP_HOST'])) {
        $host = $_SERVER['HTTP_HOST'];
    } else {
        /* 端口 */
        if (isset($_SERVER['SERVER_PORT'])) {
            $port = ':' . $_SERVER['SERVER_PORT'];
            if ((':80' == $port && 'http://' == $protocol) || (':443' == $port && 'https://' == $protocol)) {
                $port = '';
            }
        } else {
            $port = '';
        }
        if (isset($_SERVER['SERVER_NAME'])) {
            $host = $_SERVER['SERVER_NAME'] . $port;
        } elseif (isset($_SERVER['SERVER_ADDR'])) {
            $host = $_SERVER['SERVER_ADDR'] . $port;
        }
    }
    return $protocol . $host;
}

/**
 * 获得当前格林威治时间的时间戳
 *
 * @return  integer
 */
function gmtime() {
    return (time() - date('Z'));
}

/**
 * 将GMT时间戳转化为当前设置时间的时间戳
 *
 * @param  integer      $time       该参数必须是一个GMT的时间戳
 *
 * @return  integer
 */
function localtotime($time){
	$timezone = C('DEFAULT_TIMEZONE');
	if ($time === NULL) {
		$time = gmtime();
	} elseif ($time <= 0) {
		return 0;
	}

	$time += (C($timezone) * 3600);
	return $time;
}
/**
 * 将GMT时间戳格式化为用户自定义时区日期
 *
 * @param  string       $format
 * @param  integer      $time       该参数必须是一个GMT的时间戳
 *
 * @return  string
 */
function local_date($format, $time = NULL) {
    $timezone = C('DEFAULT_TIMEZONE');

    if ($time === NULL) {
        $time = gmtime();
    } elseif ($time <= 0) {
        return '';
    }
    $time += (C($timezone) * 3600);
    return date($format, $time);
}



/**
 * 处理序列化的支付、配送的配置参数
 * 返回一个以name为索引的数组
 *
 * @access  public
 * @param   string       $cfg
 * @return  void
 */
function unserialize_config($cfg) {
    if (is_string($cfg) && ($arr = unserialize($cfg)) !== false) {
        $config = array();

        foreach ($arr AS $key => $val) {
            $config[$val['name']] = $val['value'];
        }

        return $config;
    } else {
        return false;
    }
}


/**
 * 格式化重量：小于1千克用克表示，否则用千克表示
 * @param   float   $weight     重量
 * @return  string  格式化后的重量
 */
function formated_weight($weight) {
    $weight = round(floatval($weight), 3);
    if ($weight > 0) {
        if ($weight < 1) {
            /* 小于1千克，用克表示 */
            return intval($weight * 1000) . '克';
        } else {
            /* 大于1千克，用千克表示 */
            return $weight . '千克';
        }
    } else {
        return 0;
    }
}

/**
 * 计算运费
 * @param   string  $shipping_code      配送方式代码
 * @param   mix     $shipping_config    配送方式配置信息
 * @param   float   $goods_weight       商品重量
 * @param   float   $goods_amount       商品金额
 * @param   float   $goods_number       商品数量
 * @return  float   运费
 */
function shipping_fee($shipping_code, $shipping_config, $goods_weight, $goods_amount, $goods_number = '') {
    if (!is_array($shipping_config)) {
        $shipping_config = unserialize($shipping_config);
    }

    $filename = ROOT_PATH . 'ThinkPHP/Library/Com/Shipping/' . $shipping_code . '.php';
    if (file_exists($filename)) {
        include_once($filename);

        $obj = new $shipping_code($shipping_config);

        return $obj->calculate($goods_weight, $goods_amount, $goods_number);
    } else {
        return 0;
    }
}

/**
 * 获取指定配送的保价费用
 *
 * @access  public
 * @param   string      $shipping_code  配送方式的code
 * @param   float       $goods_amount   保价金额
 * @param   mix         $insure         保价比例
 * @return  float
 */
function shipping_insure_fee($shipping_code, $goods_amount, $insure) {
    if (strpos($insure, '%') === false) {
        /* 如果保价费用不是百分比则直接返回该数值 */
        return floatval($insure);
    } else {
        $path = ROOT_PATH . 'ThinkPHP/Library/Com/Shippinginsure/' . $shipping_code . '.php';

        if (file_exists($path)) {
            include_once($path);

            $shipping = new $shipping_code;
            $insure = floatval($insure) / 100;

            if (method_exists($shipping, 'calculate_insure')) {
                return $shipping->calculate_insure($goods_amount, $insure);
            } else {
                return ceil($goods_amount * $insure);
            }
        } else {
            return false;
        }
    }
}

/**
 * 计算积分的价值（能抵多少钱）
 * @param   int     $integral   积分
 * @return  float   积分价值
 */
function value_of_integral($integral) {
    $scale = floatval(C('integral_scale'));

    return $scale > 0 ? round(($integral / 100) * $scale, 2) : 0;
}

/**
 * 计算指定的金额需要多少积分
 *
 * @access  public
 * @param   integer $value  金额
 * @return  void
 */
function integral_of_value($value) {
    $scale = floatval(C('integral_scale'));

    return $scale > 0 ? round($value / $scale * 100) : 0;
}


/**
 *  生成一个用户自定义时区日期的GMT时间戳
 *
 * @access  public
 * @param   int     $hour
 * @param   int     $minute
 * @param   int     $second
 * @param   int     $month
 * @param   int     $day
 * @param   int     $year
 *
 * @return void
 */
function local_mktime($hour = NULL, $minute = NULL, $second = NULL, $month = NULL, $day = NULL, $year = NULL) {
    $timezone = isset($_SESSION['timezone']) ? $_SESSION['timezone'] : C('timezone');

    /**
     * $time = mktime($hour, $minute, $second, $month, $day, $year) - date('Z') + (date('Z') - $timezone * 3600)
     * 先用mktime生成时间戳，再减去date('Z')转换为GMT时间，然后修正为用户自定义时间。以下是化简后结果
    */
    $time = mktime($hour, $minute, $second, $month, $day, $year) - $timezone * 3600;

    return $time;
}


/**
 * 检查是否是微信浏览器访问
 */
function is_wechat_browser()
{
    $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    if (strpos($user_agent, 'micromessenger') === false) {
        return false;
    } else {
        return true;
    }
}

/* * ********************************************************
 * 加密解密函数
 * ******************************************************** */

/**
 * 加密函数
 * @param   string  $str    加密前的字符串
 * @param   string  $key    密钥
 * @return  string  加密后的字符串
 */
function encrypt($str, $key = AUTH_KEY) {
	$coded = '';
	$keylength = strlen($key);

	for ($i = 0, $count = strlen($str); $i < $count; $i += $keylength) {
		$coded .= substr($str, $i, $keylength) ^ $key;
	}

	return str_replace('=', '', base64_encode($coded));
}

/**
 * 解密函数
 * @param   string  $str    加密后的字符串
 * @param   string  $key    密钥
 * @return  string  加密前的字符串
 */
function decrypt($str, $key = AUTH_KEY) {
	$coded = '';
	$keylength = strlen($key);
	$str = base64_decode($str);

	for ($i = 0, $count = strlen($str); $i < $count; $i += $keylength) {
		$coded .= substr($str, $i, $keylength) ^ $key;
	}

	return $coded;
}

/**
 * 截取UTF-8编码下字符串的函数
 *
 * @param   string      $str        被截取的字符串
 * @param   int         $length     截取的长度
 * @param   bool        $append     是否附加省略号
 *
 * @return  string
 */
function sub_str($str, $length = 0, $append = true) {
    $str = trim($str);
    $strlength = strlen($str);

    if ($length == 0 || $length >= $strlength) {
        return $str;
    } elseif ($length < 0) {
        $length = $strlength + $length;
        if ($length < 0) {
            $length = $strlength;
        }
    }

    if (function_exists('mb_substr')) {
        $newstr = mb_substr($str, 0, $length, C('DEFAULT_CHARSET'));
    } elseif (function_exists('iconv_substr')) {
        $newstr = iconv_substr($str, 0, $length, C('DEFAULT_CHARSET'));
    } else {
        //$newstr = trim_right(substr($str, 0, $length));
        $newstr = substr($str, 0, $length);
    }

    if ($append && $str != $newstr) {
        $newstr .= '...';
    }

    return $newstr;
}


/**
 * 获得订单需要支付的支付费用
 *
 * @access  public
 * @param   integer $payment_id
 * @param   float   $order_amount
 * @param   mix     $cod_fee
 * @return  float
 */
function pay_fee($payment_id, $order_amount, $cod_fee = null) {
    $pay_fee = 0;
    $payment = D('Cart')->payment_info($payment_id);
    $rate = ($payment['is_cod'] && !is_null($cod_fee)) ? $cod_fee : $payment['pay_fee'];

    if (strpos($rate, '%') !== false) {
        /* 支付费用是一个比例 */
        $val = floatval($rate) / 100;
        $pay_fee = $val > 0 ? $order_amount * $val / (1 - $val) : 0;
    } else {
        $pay_fee = floatval($rate);
    }

    return round($pay_fee, 2);
}


/**
 *  将一个字串中含有全角的数字字符、字母、空格或'%+-()'字符转换为相应半角字符
 *
 * @access  public
 * @param   string       $str         待转换字串
 *
 * @return  string       $str         处理后字串
 */
function make_semiangle($str) {
    $arr = array('０' => '0', '１' => '1', '２' => '2', '３' => '3', '４' => '4',
        '５' => '5', '６' => '6', '７' => '7', '８' => '8', '９' => '9',
        'Ａ' => 'A', 'Ｂ' => 'B', 'Ｃ' => 'C', 'Ｄ' => 'D', 'Ｅ' => 'E',
        'Ｆ' => 'F', 'Ｇ' => 'G', 'Ｈ' => 'H', 'Ｉ' => 'I', 'Ｊ' => 'J',
        'Ｋ' => 'K', 'Ｌ' => 'L', 'Ｍ' => 'M', 'Ｎ' => 'N', 'Ｏ' => 'O',
        'Ｐ' => 'P', 'Ｑ' => 'Q', 'Ｒ' => 'R', 'Ｓ' => 'S', 'Ｔ' => 'T',
        'Ｕ' => 'U', 'Ｖ' => 'V', 'Ｗ' => 'W', 'Ｘ' => 'X', 'Ｙ' => 'Y',
        'Ｚ' => 'Z', 'ａ' => 'a', 'ｂ' => 'b', 'ｃ' => 'c', 'ｄ' => 'd',
        'ｅ' => 'e', 'ｆ' => 'f', 'ｇ' => 'g', 'ｈ' => 'h', 'ｉ' => 'i',
        'ｊ' => 'j', 'ｋ' => 'k', 'ｌ' => 'l', 'ｍ' => 'm', 'ｎ' => 'n',
        'ｏ' => 'o', 'ｐ' => 'p', 'ｑ' => 'q', 'ｒ' => 'r', 'ｓ' => 's',
        'ｔ' => 't', 'ｕ' => 'u', 'ｖ' => 'v', 'ｗ' => 'w', 'ｘ' => 'x',
        'ｙ' => 'y', 'ｚ' => 'z',
        '（' => '(', '）' => ')', '〔' => '[', '〕' => ']', '【' => '[',
        '】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']',
        '‘' => '[', '’' => ']', '｛' => '{', '｝' => '}', '《' => '<',
        '》' => '>',
        '％' => '%', '＋' => '+', '—' => '-', '－' => '-', '～' => '-',
        '：' => ':', '。' => '.', '、' => ',', '，' => '.', '、' => '.',
        '；' => ',', '？' => '?', '！' => '!', '…' => '-', '‖' => '|',
        '”' => '"', '’' => '`', '‘' => '`', '｜' => '|', '〃' => '"',
        '　' => ' ');

    return strtr($str, $arr);
}

/**
 * 得到新订单号
 * @return  string
 */
function get_order_sn() {
    /* 选择一个随机的方案 */
    mt_srand((double) microtime() * 1000000);

    return date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
}

/**
 *  将一个用户自定义时区的日期转为GMT时间戳
 *
 * @access  public
 * @param   string      $str
 *
 * @return  integer
 */
function local_strtotime($str) {
    $timezone = C('DEFAULT_TIMEZONE');

    $time = strtotime($str) - (C($timezone) * 3600);
    return $time;
}

//function local_strtotime($str) {
//    $timezone = isset($_SESSION['timezone']) ? $_SESSION['timezone'] : C('timezone');
//
//    /**
//     * $time = mktime($hour, $minute, $second, $month, $day, $year) - date('Z') + (date('Z') - $timezone * 3600)
//     * 先用mktime生成时间戳，再减去date('Z')转换为GMT时间，然后修正为用户自定义时间。以下是化简后结果
//    * */
//    $time = strtotime($str) - $timezone * 3600;
//
//    return $time;
//}

/**
 * 调用使用UCenter插件时的函数
 *
 * @param   string  $func
 * @param   array   $params
 *
 * @return  mixed
 */
function user_uc_call($func, $params = null) {
    $integrate_code = C('integrate_code');
    if (isset($integrate_code) && C('integrate_code') == 'ucenter') {
        restore_error_handler();
        $res = call_user_func_array($func, $params);
        set_error_handler('exception_handler');
        return $res;
    } else {
        return;
    }
}


/**
 * 获取订单可选配送时间 [ 此函数已废弃 ]
 * @param number $today 0/2:次日达区域   1:当日达区域  6：一天两送  :5：一送行
 * @return array $Distribution_time 配送时间列表
 */
function distributie_time_list($today=1){
	//查询送货时间
	$now_hour = date('H',time()).date('i',time());
	$Distribution_time = array();
	if($today==1){
		if($now_hour>=2000||$now_hour<0){
			//此时间段只能明日派送
			$Distribution_time['tomorrow'] =array(
					array(
                        "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 10:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 12:00:00"),
                        "title" =>"明日 10:00 - 12:00"
					),
					array(
                        "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 12:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 14:00:00"),
                        "title" =>"明日 12:00 - 14:00"
					),
					array(
                        "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 14:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00"),
                        "title" =>"明日 14:00 - 16:00"
					),
					array(
                        "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 18:00:00"),
                        "title" =>"明日 16:00 - 18:00"
					),
					array(
                        "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 18:30:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 20:30:00"),
                        "title" =>"明日 18:30 - 20:30"
					),
                    array(
                        "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 20:30:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                        "title" =>"明日 20:30 - 22:00"
                    ),
                    array(
                        "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 10:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                        "title" =>"明日 10:30 - 22:00"
                    )
			);
			$Distribution_time['today'] =array(
			);

		}
		if($now_hour>=0&&$now_hour<900){
			//此时间段只能明日派送
			$Distribution_time['tomorrow'] =array(
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 10:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 12:00:00"),
                    "title" =>"明日 10:00 - 12:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 12:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 14:00:00"),
                    "title" =>"明日 12:00 - 14:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 14:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00"),
                    "title" =>"明日 14:00 - 16:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 18:00:00"),
                    "title" =>"明日 16:00 - 18:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 18:30:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 20:30:00"),
                    "title" =>"明日 18:30 - 20:30"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 20:30:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                    "title" =>"明日 20:30 - 22:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 10:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                    "title" =>"明日 10:30 - 22:00"
                )
			);
			$Distribution_time['today'] = array(
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 10:00:00")."-".strtotime(date("Y-m-d", time()) . " 12:00:00"),
                    "title" => "今日 10:00 - 12:00"
                ),
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 12:00:00")."-".strtotime(date("Y-m-d", time()) . " 14:00:00"),
                    "title" => "今日 12:00 - 14:00"
                ),
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 14:00:00")."-".strtotime(date("Y-m-d", time()) . " 16:00:00"),
                    "title" => "今日 14:00 - 16:00"
                ),
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 16:00:00")."-".strtotime(date("Y-m-d", time()) . " 18:00:00"),
                    "title" => "今日 16:00 - 18:00"
                ),
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 18:30:00")."-".strtotime(date("Y-m-d", time()) . " 20:30:00"),
                    "title" => "今日 18:30 - 20:30"
                ),
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 20:30:00")."-".strtotime(date("Y-m-d", time()) . " 22:00:00"),
                    "title" => "今日 20:30 - 22:00"
                )

			);
		}
		//更新线上
		if($now_hour>=900&&$now_hour<1100){
			//此时间段只能明日派送
			$Distribution_time['tomorrow'] =array(
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 10:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 12:00:00"),
                    "title" =>"明日 10:00 - 12:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 12:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 14:00:00"),
                    "title" =>"明日 12:00 - 14:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 14:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00"),
                    "title" =>"明日 14:00 - 16:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 18:00:00"),
                    "title" =>"明日 16:00 - 18:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 18:30:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 20:30:00"),
                    "title" =>"明日 18:30 - 20:30"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 20:30:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                    "title" =>"明日 20:30 - 22:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 10:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                    "title" =>"明日 10:30 - 22:00"
                )
			);
			$Distribution_time['today'] = array(
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 12:00:00")."-".strtotime(date("Y-m-d", time()) . " 14:00:00"),
                    "title" => "今日 12:00 - 14:00"
                ),
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 14:00:00")."-".strtotime(date("Y-m-d", time()) . " 16:00:00"),
                    "title" => "今日 14:00 - 16:00"
                ),
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 16:00:00")."-".strtotime(date("Y-m-d", time()) . " 18:00:00"),
                    "title" => "今日 16:00 - 18:00"
                ),
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 18:30:00")."-".strtotime(date("Y-m-d", time()) . " 20:30:00"),
                    "title" => "今日 18:30 - 20:30"
                ),
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 20:30:00")."-".strtotime(date("Y-m-d", time()) . " 22:00:00"),
                    "title" => "今日 20:30 - 22:00"
                )
			);

		}
		if($now_hour>=1100&&$now_hour<1300){
			//此时间段只能明日派送
			$Distribution_time['tomorrow'] =array(
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 10:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 12:00:00"),
                    "title" =>"明日 10:00 - 12:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 12:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 14:00:00"),
                    "title" =>"明日 12:00 - 14:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 14:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00"),
                    "title" =>"明日 14:00 - 16:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 18:00:00"),
                    "title" =>"明日 16:00 - 18:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 18:30:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 20:30:00"),
                    "title" =>"明日 18:30 - 20:30"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 20:30:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                    "title" =>"明日 20:30 - 22:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 10:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                    "title" =>"明日 10:30 - 22:00"
                )
			);
			$Distribution_time['today'] = array(
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 14:00:00")."-".strtotime(date("Y-m-d", time()) . " 16:00:00"),
                    "title" => "今日 14:00 - 16:00"
                ),
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 16:00:00")."-".strtotime(date("Y-m-d", time()) . " 18:00:00"),
                    "title" => "今日 16:00 - 18:00"
                ),
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 18:30:00")."-".strtotime(date("Y-m-d", time()) . " 20:30:00"),
                    "title" => "今日 18:30 - 20:30"
                ),
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 20:30:00")."-".strtotime(date("Y-m-d", time()) . " 22:00:00"),
                    "title" => "今日 20:30 - 22:00"
                )
			);
		}
		if($now_hour>=1300&&$now_hour<1500){
			//此时间段只能明日派送
			$Distribution_time['tomorrow'] =array(
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 10:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 12:00:00"),
                    "title" =>"明日 10:00 - 12:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 12:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 14:00:00"),
                    "title" =>"明日 12:00 - 14:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 14:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00"),
                    "title" =>"明日 14:00 - 16:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 18:00:00"),
                    "title" =>"明日 16:00 - 18:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 18:30:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 20:30:00"),
                    "title" =>"明日 18:30 - 20:30"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 20:30:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                    "title" =>"明日 20:30 - 22:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 10:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                    "title" =>"明日 10:30 - 22:00"
                )
			);
			$Distribution_time['today'] = array(
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 16:00:00")."-".strtotime(date("Y-m-d", time()) . " 18:00:00"),
                    "title" => "今日 16:00 - 18:00"
                ),
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 18:30:00")."-".strtotime(date("Y-m-d", time()) . " 20:30:00"),
                    "title" => "今日 18:30 - 20:30"
                ),
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 20:30:00")."-".strtotime(date("Y-m-d", time()) . " 22:00:00"),
                    "title" => "今日 20:30 - 22:00"
                )
			);

		}
		if($now_hour>=1500&&$now_hour<1730){
			//
			$Distribution_time['tomorrow'] =array(
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 10:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 12:00:00"),
                    "title" =>"明日 10:00 - 12:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 12:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 14:00:00"),
                    "title" =>"明日 12:00 - 14:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 14:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00"),
                    "title" =>"明日 14:00 - 16:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 18:00:00"),
                    "title" =>"明日 16:00 - 18:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 18:30:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 20:30:00"),
                    "title" =>"明日 18:30 - 20:30"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 20:30:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                    "title" =>"明日 20:30 - 22:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 10:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                    "title" =>"明日 10:30 - 22:00"
                )
			);

			$Distribution_time['today'] = array(
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 18:30:00")."-".strtotime(date("Y-m-d", time()) . " 20:30:00"),
                    "title" => "今日 18:30 - 20:30"
                ),
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 20:30:00")."-".strtotime(date("Y-m-d", time()) . " 22:00:00"),
                    "title" => "今日 20:30 - 22:00"
                )
			);

		}
        if($now_hour>=1730&&$now_hour<2000){
            //
            $Distribution_time['tomorrow'] =array(
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 10:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 12:00:00"),
                    "title" =>"明日 10:00 - 12:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 12:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 14:00:00"),
                    "title" =>"明日 12:00 - 14:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 14:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00"),
                    "title" =>"明日 14:00 - 16:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 18:00:00"),
                    "title" =>"明日 16:00 - 18:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 18:30:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 20:30:00"),
                    "title" =>"明日 18:30 - 20:30"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 20:30:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                    "title" =>"明日 20:30 - 22:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 10:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                    "title" =>"明日 10:30 - 22:00"
                )
            );

            $Distribution_time['today'] = array(
                array(
                    "value" => strtotime(date("Y-m-d", time()) . " 20:30:00")."-".strtotime(date("Y-m-d", time()) . " 22:00:00"),
                    "title" => "今日 20:30 - 22:00"
                )
            );

        }
	}
    elseif ($today==0||$today == 2)
    {
        //次日达区域配送时间
        $Distribution_time['tomorrow'] =array(
            array(
                "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 10:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                "title" =>"明日 10:00 - 22:00"
            )
        );
        $Distribution_time['today'] =array(
        );
    }
	elseif ($today == 5)
    {
        //一天一送
        if($now_hour>=1400){
            //此时间段只能明日派送
            $Distribution_time['tomorrow'] =array(
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                    "title" =>"明日 16:00 - 22:00"
                )
            );
            $Distribution_time['today'] =array(
            );
        }else{
            //今日明日都可以派送
            $Distribution_time['tomorrow'] =array(
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                    "title" =>"明日 16:00 - 22:00"
                )
            );
            $Distribution_time['today'] =array(
                array(
                    "value" =>strtotime(date("Y-m-d",time())." 16:00:00")."-".strtotime(date("Y-m-d",time())." 22:00:00"),
                    "title" =>"今日 16:00 - 22:00"
                )
            );
        }

    }
    elseif ($today == 6)
    {
        //一天二送
        if($now_hour>=1400){
            //此时间段只能明日派送
            $Distribution_time['tomorrow'] =array(
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 10:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 14:00:00"),
                    "title" =>"明日 10:00 - 14:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                    "title" =>"明日 16:00 - 22:00"
                )
            );
            $Distribution_time['today'] =array(
            );
        }
        if($now_hour<=900){
            //九点之前可选择今日和明日派送
            $Distribution_time['tomorrow'] =array(
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 10:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 14:00:00"),
                    "title" =>"今日 10:00 - 14:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                    "title" =>"今日 16:00 - 22:00"
                )
            );
            $Distribution_time['today'] =array(
                array(
                    "value" =>strtotime(date("Y-m-d",time())." 10:00:00")."-".strtotime(date("Y-m-d",time())." 14:00:00"),
                    "title" =>"今日 10:00 - 14:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",time())." 16:00:00")."-".strtotime(date("Y-m-d",time())." 22:00:00"),
                    "title" =>"今日 16:00 - 22:00"
                )
            );
        }
        if($now_hour>900&&$now_hour<1400){
            //此时间段只能明日派送
            $Distribution_time['tomorrow'] =array(
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 10:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 14:00:00"),
                    "title" =>"明日 10:00 - 14:00"
                ),
                array(
                    "value" =>strtotime(date("Y-m-d",strtotime("+1 day"))." 16:00:00")."-".strtotime(date("Y-m-d",strtotime("+1 day"))." 22:00:00"),
                    "title" =>"明日 16:00 - 22:00"
                )
            );
            $Distribution_time['today'] =array(
                array(
                    "value" =>strtotime(date("Y-m-d",time())." 16:00:00")."-".strtotime(date("Y-m-d",time())." 22:00:00"),
                    "title" =>"今日 16:00 - 22:00"
                )
            );
        }
	}else{
	    //未知送达方式
        $Distribution_time['tomorrow'] =array();
        $Distribution_time['today'] =array();
    }

	return $Distribution_time;
}
/*
 * 获取推荐引擎推荐商品列表
 * @param $user_id 用户id
 * @param $item_id 商品id (有此参数时调详情推荐场景否则调首页推荐场景)
 * return array
 */
function get_recommend_shop($url, $content=''){
    $akId = C('ALIYUN_AKID');
    $akSecret = C('ALIYUN_AKSECRET');

    $method = 'GET';
    if($content){
        $content = json_encode($content);
        $method = 'POST';
    }

    $options = array(
        'http' => array(
            'header' => array(
                'accept'=> "application/json",
                'content-type'=> "application/json",
                'date'=> gmdate("D, d M Y H:i:s \G\M\T"),
                'authorization' => ''
            ),
            'method' => $method, //可以是 GET, POST, DELETE, PUT
            'content' => $content
        )
    );
    $http = $options['http'];
    $header = $http['header'];
    $urlObj = parse_url($url);
    if(empty($urlObj["query"]))
        $path = $urlObj["path"];
    else
        $path = $urlObj["path"]."?".$urlObj["query"];
    $body = $http['content'];
    if(empty($body))
        $bodymd5 = $body;
    else
        $bodymd5 = base64_encode(md5($body,true));
    $stringToSign = $http['method']."\n".$header['accept']."\n".$bodymd5."\n".$header['content-type']."\n".$header['date']."\n".$path;
    $signature = base64_encode(
        hash_hmac(
            "sha1",
            $stringToSign,
            $akSecret, true));
    $authHeader = "Dataplus "."$akId".":"."$signature";
    $options['http']['header']['authorization'] = $authHeader;
    $options['http']['header'] = implode(
        array_map(
            function($key, $val){
                return $key.":".$val."\r\n";
            },
            array_keys($options['http']['header']),
            $options['http']['header']));
    $context = stream_context_create($options);
    $file = file_get_contents($url, false, $context );
    $data = json_decode($file,true);
    if($data['code'] == 'SUCCESS'){
        return $data['data'];
    }
    return $data;
}

/*
 * 获取访问来源
 * Created By caicm
 */
function get_source(){
    $is_h5 = I('post.is_h5'); //h5
    $is_ios = I('post.is_ios'); //ios
    $is_app = I('post.is_app'); //android
    $h5_src = I('post.h5_src'); //h5 微信分享记录 H5-P H5-Q  H5-H
    $IS_HD = I("post.IS_HD");
    if($is_h5){
        $source = $h5_src ? $h5_src : 'h5';
    }elseif($is_ios){
        $source = 'ios';
    }elseif($is_app){
        $source = 'android';
    }elseif($IS_HD) {
        $source = 'hd';
    }else{
        $source = 'h5';
    }
    return $source;
}

/**
 * 检查APP版本限制
 * @author Min
 * @param $android_version
 * @param $ios_version
 * @return bool
 */
function check_app_version($android_version, $ios_version)
{
    $version = '';
    $source = get_source();
    $app_version = I('version', '');

    if ($source == 'ios') {
        $version = $ios_version;
    } elseif ($source == 'android') {
        $version = $android_version;
    }

    if ($version > $app_version && $version) {
        return true;
    }

    return false;
}

/**
 * 自动规划路径
 * @param string $start_address 起始点
 * @param array $way_array  途径点
 * @param int $way  行车类型 1:驾车 2:骑行  3:步行
 * @param int $mode  计算类型 1:时间  2:距离
 * @return array    重新排序点
 */
function auto_planning($start_address, $way_array=array(),$way=1,$mode=1){
    //定义百度API地址和AK
    //百度 Web服务API 地址  http://lbsyun.baidu.com/index.php?title=webapi/route-matrix-api-v2
    switch ($way){
        case 1:
            $now_way = "driving";
            break;
        case 2:
            $now_way = "riding";
            break;
        case 3:
            $now_way = "walking";
            break;
        default:
            return false;
    }
    switch ($mode){
        case 1:
            $determination_mode = "duration";
            break;
        case 2:
            $determination_mode = "distance";
            break;
        default:
            return false;
    }
    $url = "https://api.map.baidu.com/routematrix/v2/".$now_way."?output=json&ak=".C('BAIDU_KEY')."&";

    //重新排序坐标数组 包含起始点
    $complete_array = array();
    $complete_array[] = $start_address;

    $max = count($way_array);
    for($i=0;$i<$max;$i++){
        $start_address_now = end($complete_array);
        if($i==0){
            $way_array_s = $way_array;  //本次运算输入的坐标组
            unset($way_array_s[0]);
        }else{
            $way_array_s = $way_array;  //本次运算输入的坐标组
        }
        $time_url = $url."origins=".$start_address_now."&destinations=".implode("|",$way_array_s);
        //echo "-----------------------------------------------------------<br/>";
        //echo $time_url."<br>";
        $get_data = file_get_contents($time_url);
        $Json_Data = json_decode($get_data, true);

        $data= $Json_Data['result'];
        $old_key = 0;
        foreach ($data as $key=>$value){
            if($data[$old_key][$determination_mode]['value']>$data[$key][$determination_mode]['value']){
                $old_key = $key;
                //比较出此次循环最近距离值
                //echo "预算值：".$data[$old_key]['duration']['value']."--".$data[$key]['duration']['value']."<br>";
                //echo "预算KEY：".$old_key."<br>";
            }
        }

        $new_s_array = array_values($way_array_s);  //清理数组下标
        //echo "重新排序值：<br>";
        //dump($new_s_array);
        $complete_array[] = $new_s_array[$old_key]; //将本次最小距离值放入结果数组
        //echo "处理结束数据：<br>";
        //dump($new_array);
        foreach ($way_array as $key=>$value){
            if($new_s_array[$old_key] == $value){
                unset($way_array[$key]);    //删除原数组中本次最小值
            }
        }
        //echo "还剩余数据：<br>";
        //dump($way_array);
    }
    //dump($new_array);
    return $complete_array;
}

/**
 * 转换字符串形式的时间表达式为GMT时间戳
 *
 * @param   string  $str
 *
 * @return  integer
 */
function gmstr2time($str)
{
    $time = strtotime($str);

    if ($time > 0)
    {
        $time -= date('Z');
    }

    return $time;
}

/**
 * 转换时分结构字符串（例：09:30）
 * @param $time
 * @return string
 */
function convert_to_time($time){
    if(!empty($time)){
        if(strlen($time)<4){
            $time = '0'.strval($time);
        }
        $time = substr($time,0,2).":".substr($time,2,2);
    }

    return $time;
}


/**
 * GCJ02 转 BD09
 * @param $lat
 * @param $lng
 * @return array
 */
function Convert_GCJ02_To_BD09($lat, $lng)
{
    $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    $x = $lng;
    $y = $lat;
    $z =sqrt($x * $x + $y * $y) + 0.00002 * sin($y * $x_pi);
    $theta = atan2($y, $x) + 0.000003 * cos($x * $x_pi);
    $lng = $z * cos($theta) + 0.0065;
    $lat = $z * sin($theta) + 0.006;
    return array('longitude'=>$lng,'latitude'=>$lat);
}
/**
 * BD09 转 GCJ02
 * @param $lat
 * @param $lng
 * @return array
 */
function Convert_BD09_To_GCJ02($lat,$lng)
{
    $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    $x = $lng - 0.0065;
    $y = $lat - 0.006;
    $z = sqrt($x * $x+$y * $y) - 0.00002 * sin($y * $x_pi);
    $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
    $data['longitude'] = $z * cos($theta);
    $data['latitude'] = $z * sin($theta);
    return $data;
}
function get_order_state(){
    /* 订单状态 */
    $_LANG['cs'][OS_UNCONFIRMED] = '待确认';
    $_LANG['cs'][CS_AWAIT_PAY] = '待付款';
    $_LANG['cs'][103] = '已付款';
    $_LANG['cs'][CS_AWAIT_SHIP] = '待发货';
    $_LANG['cs'][CS_FINISHED] = '已完成';
    $_LANG['cs'][PS_PAYING] = '付款中';
    $_LANG['cs'][OS_CANCELED] = '取消';
    $_LANG['cs'][OS_INVALID] = '无效';
    $_LANG['cs'][OS_RETURNED] = '退货';
    $_LANG['cs'][OS_SHIPPED_PART] = '部分发货';

    /* 订单状态 */
    $_LANG['os'][OS_UNCONFIRMED] = '未确认';
    $_LANG['os'][OS_CONFIRMED] = '已确认';
    $_LANG['os'][OS_CANCELED] = ' 取消';
    $_LANG['os'][OS_INVALID] = '无效';
    $_LANG['os'][OS_RETURNED] = '退货';
    $_LANG['os'][OS_SPLITED] = '已分单';
    $_LANG['os'][OS_SPLITING_PART] = '部分分单';

    $_LANG['ss'][SS_UNSHIPPED] = '未发货';
    $_LANG['ss'][SS_PREPARING] = '配货中';
    $_LANG['ss'][SS_SHIPPED] = '已发货';
    $_LANG['ss'][SS_RECEIVED] = '收货确认';
    $_LANG['ss'][SS_SHIPPED_PART] = '已发货(部分商品)';
    $_LANG['ss'][SS_SHIPPED_ING] = '发货中';
    $_LANG['ss'][7] = '配货中'; // 已分单
    $_LANG['ss'][6] = '送达未确认'; // 已分单

    $_LANG['ps'][PS_UNPAYED] = '未付款';
    $_LANG['ps'][PS_PAYING] = '付款中';
    $_LANG['ps'][PS_PAYED] = '已付款';
    $_LANG['ps'][PS_PAYED_PART] = '部分退款';
    $_LANG['ps'][PS_PAYED_ALL] = '全部退款';

    $_LANG['cg'][0] = '未出仓';
    $_LANG['cg'][1] = '已出仓';
    $_LANG['cg'][2] = '应回仓';

    $_LANG['dd'][0] = '未到站';
    $_LANG['dd'][1] = '已到站';
    $_LANG['dd'][2] = '已出站';
    $_LANG['dd'][3] = '需回仓';
    return $_LANG;
}

function write_log($message,$filename){
    $log = new \Think\Log();
    $destination = C('LOG_PATH').date('y_m_d').'_'.$filename.'.log';

    $log->write($message,'DEBUG','',$destination);
}
/**
 * 求两个已知经纬度之间的距离,单位为米
 *
 * @param $lng1,$lng2 经度
 * @param $lat1,$lat2 纬度
 * @return float 距离，单位米
 * @author
 */
function get_distance($lng1, $lat1, $lng2, $lat2) {
    // 将角度转为狐度
    $radLat1 = deg2rad($lat1); //deg2rad()函数将角度转换为弧度
    $radLat2 = deg2rad($lat2);
    $radLng1 = deg2rad($lng1);
    $radLng2 = deg2rad($lng2);
    $a = $radLat1 - $radLat2;
    $b = $radLng1 - $radLng2;
    $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
    return $s;
}

function trimall($str)//删除空格
{
    $qian=array(" ","　","\t","\n","\r");$hou=array("","","","","");
    return str_replace($qian,$hou,$str);
}

function cRedis(){
    $redis = new \Redis(); 
    $redis->connect('47.106.126.134','6379');
    $redis->auth('SDX2017best');
    $redis->select('5');
    return $redis;
}

if( ! function_exists('array_column'))
{
    function array_column($input, $columnKey, $indexKey = NULL)
    {
        $columnKeyIsNumber = (is_numeric($columnKey)) ? TRUE : FALSE;
        $indexKeyIsNull = (is_null($indexKey)) ? TRUE : FALSE;
        $indexKeyIsNumber = (is_numeric($indexKey)) ? TRUE : FALSE;
        $result = array();

        foreach ((array)$input AS $key => $row)
        {
            if ($columnKeyIsNumber)
            {
                $tmp = array_slice($row, $columnKey, 1);
                $tmp = (is_array($tmp) && !empty($tmp)) ? current($tmp) : NULL;
            }
            else
            {
                $tmp = isset($row[$columnKey]) ? $row[$columnKey] : NULL;
            }
            if ( ! $indexKeyIsNull)
            {
                if ($indexKeyIsNumber)
                {
                    $key = array_slice($row, $indexKey, 1);
                    $key = (is_array($key) && ! empty($key)) ? current($key) : NULL;
                    $key = is_null($key) ? 0 : $key;
                }
                else
                {
                    $key = isset($row[$indexKey]) ? $row[$indexKey] : 0;
                }
            }

            $result[$key] = $tmp;
        }

        return $result;
    }
}